package bsc.war.ajax;

import bsc.ejb.entity.Map;
import bsc.ejb.entity.Person;
import bsc.ejb.entity.ScoreDomain;
import bsc.ejb.entity.PersonScoreDomain;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.apache.commons.lang.math.NumberUtils;

/**
 *
 * @author castrosalazr
 */
public class DesignerAddDomain extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
            String type = request.getParameter("type");

            List<Person> persons = delegate.findAvaliablePerson();
            List<Map> maps = delegate.findMap();


            ScoreDomain domain = null;
            List<Person> personsDomain = null;
            java.util.Map<Person, PersonScoreDomain> personTypes = new HashMap<Person, PersonScoreDomain>();

            Map mapDomain = null;


            if ("edit".equals(type) && NumberUtils.isDigits(request.getParameter("id"))) {
                //mav.addObject("smtp", delegate.findSMTP(Long.valueOf(request.getParameter("id"))));
                request.setAttribute("edit", Boolean.TRUE);

                domain = delegate.findScoreDomain(Long.valueOf(request.getParameter("id")));
                mapDomain = (Map) domain.getScoreView().getScoreCard();
                personsDomain = delegate.findPerson(domain);
                for (Person person : personsDomain) {
                    if (persons.contains(person)) {
                        persons.remove(person);
                    }
                }
                maps.remove(mapDomain);
                //Upload the type PersonScoreDomain of each person
                
                for (Person person : personsDomain) {
                    PersonScoreDomain personScoreDomain = delegate.findPersonScoreDomain(person, domain);
                    personTypes.put(person, personScoreDomain);
                }
            }


            PersonScoreDomain.Profile profiles[] = {PersonScoreDomain.Profile.CORE_TEAM, PersonScoreDomain.Profile.FINAL_USER};




            request.setAttribute("persons", persons);
            request.setAttribute("maps", maps);
            request.setAttribute("personsDomain", personsDomain);
            request.setAttribute("personTypes", personTypes);
            request.setAttribute("mapDomain", mapDomain);
            request.setAttribute("domain", domain);
            request.setAttribute("profiles", profiles);




        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Caught exception: " + e);
        }

        return new ModelAndView("designerAddDomain");
    }
}
